<?php
namespace app\index\controller;
use think\Controller;
use think\Db;
use think\Config;
use think\Request;

class Index extends Controller{   
   
    public function _initialize(){

        //友情链接
        $links=Db::name('link_list')->where(['status'=>"1"])->order('px desc')->field('name,link')->select();
        $this->assign('links',$links);

        //站点信息
        $this->assign('siteinfo',config('web'));

        //轮播
        $banners=Db::name('banner_list')->where('status','1')->order('px desc')->select();
        $this->assign('banners',$banners);

        //导航
        $cates=Db::name('cate_list')->where(['status'=>'1','isnav'=>'1'])->order('catepx desc')->select();
        $this->assign('cates',$cates);


    }

    public function index(){

        
        $cateid=Db::name('cate_list')->order('cateid asc')->value('cateid');

        echo  self::cate($cateid);

    }

    public function cate($CateId=""){
        
        $pid=input('pid');
        $cateid= input('cateid');
        $map=array(
            'cateid'=>isset($cateid)?$cateid:$CateId,
            'status'=>'1'
        );
      
        $cateinfo=Db::name('cate_list')->where($map)->find();
        if(empty($cateinfo)){
            $this->error("当前分类不存在!");
        }
        
        if($pid) $map['pid']=$pid;

        $list = Db::name('news_list')->where($map)->order('newsid desc')->paginate(10,false,array(
'query' => $map));

        $this->assign('list', $list);

        $this->assign('cateinfo',$cateinfo);
        
        $seo=array(
            'title'=>empty($cateinfo['catename'])?config('web.name'):$cateinfo['catename'],
            'keyword'=>empty($cateinfo['keyword'])?config('web.keyword'):$cateinfo['keyword'],
            'description'=>empty($cateinfo['description'])?config('web.description'):$cateinfo['description'],
        );
        $this->assign('seo',$seo);


        return $this->fetch($cateinfo['template']);

    }
    public function content(){

        $cateid=input('cateid');
        $newsid=input('newsid');

        $map=array(
            'cateid'=>$cateid,
            'newsid'=>$newsid,
            'status'=>'1'
        );

        Db::name('news_list')->where($map)->setInc('hits');

        

        $newsinfo=Db::name('news_list')->where($map)->find();
        if(empty($newsinfo)){
            $this->error("当前信息不存在!");
        }

        $map=array(
            'cateid'=>$newsinfo['cateid'],
            'status'=>'1'
        );
        $cateinfo=Db::name('cate_list')->where($map)->find();

        //标题
        if(!empty($newsinfo['title'])){
            $title=$newsinfo['title'];
        }else if(empty($newsinfo['title']) && !empty($cateinfo['cateinfo'])){
            $title=$cateinfo['cateinfo'];
        }else{
            $title=config('web.name');
        }

        //关键字
        if(!empty($newsinfo['keyword'])){
            $title=$newsinfo['keyword'];
        }else if(empty($newsinfo['keyword']) && !empty($cateinfo['keyword'])){
            $title=$cateinfo['keyword'];
        }else{
            $title=config('web.keyword');
        }

        //描述
        if(!empty($newsinfo['description'])){
            $title=$newsinfo['description'];
        }else if(empty($newsinfo['description']) && !empty($cateinfo['description'])){
            $title=$cateinfo['description'];
        }else{
            $title=config('web.description');
        }
        
        $seo=array(
            'title'=>$title,
            'keyword'=>$keyword,
            'description'=>$description
        );
        $this->assign('seo',$seo);

        $this->assign('newsinfo',$newsinfo);
        return $this->fetch($newsinfo['template']);
    }

    public function forms(){
       if(Request::instance()->isPost()){
            $data=input('post.');
            $data['addtime']=date('Y-m-d H:i:s',time());
            Db::name('msg_list')->insert($data);
            alert("信息提交成功，等待客服联系您~");
       }
    }

    


}